package com.nervenets.web.hibernate.dao;

import com.nervenets.web.enumeration.OrderStatus;
import com.nervenets.web.hibernate.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface OrderDao extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
    long countByUserId(long userId);

    @Query(value = "SELECT status,count(id) AS total FROM orders GROUP BY status", nativeQuery = true)
    List<Map> getCountGroupByStatus();

    Order findTop1ByUserIdAndStatus(long userId, OrderStatus orderStatus);
}
